package com.paypalpay.service.impl;

import com.paypalpay.entity.common.UserboundPaymentOrder;
import com.paypalpay.entity.pay.PaypalNotificationpaymentResults;
import com.paypalpay.mapper.UserBoundPaymentOrderMapper;
import com.paypalpay.service.UserboundPaymentOrderService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
@Transactional
public class UserboundPaymentOrderServiceImpl implements UserboundPaymentOrderService {
    @Resource
    UserBoundPaymentOrderMapper userBoundPaymentOrderMapper;

    //插入数据：此时是微信公众号访问的无卡购买流程
    @Override
    public void insertWeixinNoCardInfo(UserboundPaymentOrder user) {
        userBoundPaymentOrderMapper.insertWeixinNoCardInfo(user);
    }

    //查询数据：是否存在当前用户
    @Override
    public int findExistOrNot(String deviceId, String openId) {
        return userBoundPaymentOrderMapper.findExistOrNot(deviceId, openId);
    }

    //查询数据：是否存在当前用户，此时orderId为null和空
    @Override
    public int findExistOrNotByOrderId(String deviceId, String openId) {
        return userBoundPaymentOrderMapper.findExistOrNotByOrderId(deviceId, openId);
    }

    //插入数据：此时是标准的微信中用户绑定
    @Override
    public void userBoundWeixinAccess(UserboundPaymentOrder user) {
        userBoundPaymentOrderMapper.userBoundWeixinAccess(user);
    }

    //查询当前数据是否存在：根据设备号和设备类型去查询非微信用户访问
    @Override
    public int findExistNotWeixinUser(String deviceId, String deviceType,String requestOrderId) {
        return userBoundPaymentOrderMapper.findExistNotWeixinUser(deviceId, deviceType,requestOrderId);
    }

    //非微信用户访问
    @Override
    public void userBounNotWeixinAccess(UserboundPaymentOrder user) {
        userBoundPaymentOrderMapper.userBounNotWeixinAccess(user);
    }

    //查询当前用户是否存在,无卡流程的
    @Override
    public int findExistOrNotByTemorderId(String receiveUserName, String openId, String receiveAddress) {
        return userBoundPaymentOrderMapper.findExistOrNotByTemorderId(receiveUserName, openId, receiveAddress);
    }

    //根据获取到的套餐code去查询套餐名称
    @Override
    public String getPackageNameByPackageCode(String packageCode,String partnerCode) {
        return userBoundPaymentOrderMapper.getPackageNameByPackageCode(packageCode,partnerCode);
    }

    @Override
    public String getPackageNameBypackages(String packages,String partnerCode) {
        return userBoundPaymentOrderMapper.getPackageNameBypackages(packages, partnerCode);
    }

    // 根据收到的openId去更新数据表，此时deviceId为null
    @Override
    public void updateUserboundPaymentOrderNoCard(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.updateUserboundPaymentOrderNoCard(paymentOrder);
    }

    //微信用户访问的支付上发更新
    @Override
    public void updateUserboundWeixinPaymentOrder(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.updateUserboundWeixinPaymentOrder(paymentOrder);
    }

    //非微信用户访问的支付上发更新
    @Override
    public void updateUserboundNotWeixinPaymentOrder(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.updateUserboundNotWeixinPaymentOrder(paymentOrder);
    }

    //根据微信回调的订单号去数据库查询当前记录是不是已经记录
    @Override
    public int findOrderIdByNotify(String out_trade_no) {
        return userBoundPaymentOrderMapper.findOrderIdByNotify(out_trade_no);
    }

    //根据根据微信回调的数据入库是否成功支付，根据paymentId这个id去数据插入支付结果，成功就将PayRst设为1；不插入就数据库默认0，成功就要通知，数据库就记1，不成功就不通知数据库会默认0
    @Override
    public void updatePayRst(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.updatePayRst(paymentOrder);
    }

    //根据微信回调的订单号去数据库查询对应的orderid
    @Override
    public String queryOrderIdByPaymentId(String paymentId) {
        return userBoundPaymentOrderMapper.queryOrderIdByPaymentId(paymentId);
    }

    @Override
    public String queryCodeByPaymentId(String paymentId) {
        return userBoundPaymentOrderMapper.queryCodeByPaymentId();
    }

    //标准的非微信用户oceanpay支付上发数据，更新数据表
    @Override
    public void updateUserboundOceanPaymentOrder(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.updateUserboundOceanPaymentOrder(paymentOrder);
    }

    //微信访问，根据openid获取deviceId和deviceType
    @Override
    public List<UserboundPaymentOrder> getDeviceByOpenId(String openId) {
        return userBoundPaymentOrderMapper.getDeviceByOpenId(openId);
    }

    //去数据库查询哪些数据是需要去北京下真正的单的
    @Override
    public List<UserboundPaymentOrder> getNocardToOrderList() {
        return userBoundPaymentOrderMapper.getNocardToOrderList();
    }

    @Override
    public void updateChannelOrderID(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.updateChannelOrderID(paymentOrder);
    }

    //1，根据deviceId先去把支付订单orderId的表更新了
    @Override
    public void updateOrderIdByDeviceId(UserboundPaymentOrder order) {
        userBoundPaymentOrderMapper.updateOrderIdByDeviceId(order);
    }

    // 所有的通知结束之后，将数据库里面的isNotify变成0，即为不需要再次通知
    @Override
    public void updateIsNotify(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.updateIsNotify(paymentOrder);
    }

    //去数据库查询一下有哪些记录是需要批量回传支付结果通知的
    @Override
    public List<UserboundPaymentOrder> getNocardToNotifyList() {
        return userBoundPaymentOrderMapper.getNocardToNotifyList();
    }

    //更新微信回复的订单id去数据库查询其他数据
    @Override
    public UserboundPaymentOrder queryOrderByPaymentId(String paymentId) {
        return userBoundPaymentOrderMapper.queryOrderByPaymentId(paymentId);
    }

    //将user_bound_payment_order_t转移到user_bound_payment_order_hist_t，每小时执行一次，转移的是未转移过的数据
    @Override
    public void dataTransfer() {
        userBoundPaymentOrderMapper.dataTransfer();
    }

    //将user_bound_payment_order_t表中未支付成功的数据删除掉，当天的数据不删，每天晚上23点执行一次
    @Override
    public void originalDataDelete() {
        userBoundPaymentOrderMapper.originalDataDelete();
    }

    //根据paypal回传的订单id去数据库查询当前记录是否入库了
    @Override
    public int findRecordByInvoice(String invoice) {
        return userBoundPaymentOrderMapper.findRecordByInvoice(invoice);
    }

    //根据订单号去更新回传的订单支付信息
    @Override
    public void UpdatePaypalNotify(UserboundPaymentOrder order) {
        userBoundPaymentOrderMapper.UpdatePaypalNotify(order);
    }
    //查询数据库中需要给渠道通知的数据
    @Override
    public List<UserboundPaymentOrder> getChannelResultList() {
        return userBoundPaymentOrderMapper.getChannelResultList();
    }

    @Override
    public void updateIsChannelNotity(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.updateIsChannelNotity(paymentOrder);
    }

    @Override
    public List<UserboundPaymentOrder> getListToOpen() {
        return userBoundPaymentOrderMapper.getListToOpen();
    }

    @Override
    public void updateIsOpenNotify(UserboundPaymentOrder uOrder) {
        userBoundPaymentOrderMapper.updateIsOpenNotify(uOrder);
    }

    @Override
    public int findOrderIdAndDeviceId(String paymentOrderId, String orderId, String deviceId, String amount) {
        return userBoundPaymentOrderMapper.findOrderIdAndDeviceId(paymentOrderId,orderId,deviceId,amount);
    }

    @Override
    public void InsertUserboundNotWeixinPaymentOrder(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.InsertUserboundNotWeixinPaymentOrder(paymentOrder);
    }

    @Override
    public int findCountByOrderNumber(String orderNumber) {
        return userBoundPaymentOrderMapper.findCountByOrderNumber(orderNumber);
    }

    @Override
    public void insertPaymentResultsPaypal(PaypalNotificationpaymentResults results) {
        userBoundPaymentOrderMapper.insertPaymentResultsPaypal(results);
    }

    @Override
    public int findCountByOrderId(String out_trade_no) {
        return userBoundPaymentOrderMapper.findCountByOrderId(out_trade_no);
    }

    @Override
    public void InsertPaymentOrder(UserboundPaymentOrder paymentOrder) {
         userBoundPaymentOrderMapper.InsertPaymentOrder(paymentOrder);
    }

    @Override
    public int queryOrderByOrderId(String orderCode) {
        return userBoundPaymentOrderMapper.queryOrderByOrderId(orderCode);
    }

    @Override
    public void UpdateRenewPackagesOrder(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.UpdateRenewPackagesOrder(paymentOrder);
    }

    @Override
    public List<UserboundPaymentOrder> getListToRenewPackage() {
        return userBoundPaymentOrderMapper.getListToRenewPackage();
    }

    @Override
    public void updateIsRenew(UserboundPaymentOrder paymentOrder) {
        userBoundPaymentOrderMapper.updateIsRenew(paymentOrder);
    }

    @Override
    public void InsertUserOrder(UserboundPaymentOrder user) {

        userBoundPaymentOrderMapper.InsertUserOrder(user);

    }
}
